/* eslint-disable no-tabs */
/* eslint-disable no-mixed-spaces-and-tabs */
import $http from '../utils/http.js'
import router from '../routers/router.js'
import { Notification } from 'atelement'
const getOptions = 'group/getOptions' // 获取所有用户组名称
const setOptions = 'group/setOptions' // 设置所用用户组名称(用于筛选)
const setOptionselect = 'group/setOptionselect' // 设置所用用户组名称(用于下拉框)
const getGroup = 'group/getGroup' // 获取用户组信息（人员、名称、角色）
const setGroup = 'group/setGroup' // 设置用户组信息（人员、名称、角色）
const getGroupdetail = 'group/getGroupdetail' // 用户组详情
const setGroupdetail = 'group/setGroupdetail' // 设置用户组详情
const getRightdetail = 'group/getRightdetail' // 右侧详情信息
const setRightdetail = 'group/setRightdetail' // 设置右侧详情信息
const getRole = 'group/getRole' // 获取角色选项
const setRole = 'group/setRole' // 设置角色信息
const addGroup = 'group/addGroup' // 新建用户组
const delUsergroup = 'group/delUsergroup' // 删除用户组
const updateGroup = 'group/updateGroup' // 修改用户组
const getRolelist = 'group/getRolelist' // 获取用户角色
const setRolelist = 'group/setRolelist'
const getRoleid = 'group/getRoleid' // 获取用户角色ID
const setRoleid = 'group/setRoleid'
const addGroupflag = 'group/addGroupflag'
const upGroupflag = 'group/upGroupflag'
const groupDetailflage = 'group/groupDetailflage'
const space = 'group/space'
const getSyncgroup = 'group/getSyncgroup' // 同步信息数量(用户)
const setSyncgroup = 'group/setSyncgroup'
const getSyncmessagegroup = 'group/getSyncmessagegroup' // 同步信息(用户组)
const setSyncmessagegroup = 'group/setSyncmessagegroup'
const getSynclog = 'group/getSynclog' // 同步历史信息
const setSynclog = 'group/setSynclog'
const delSyncgroup = 'group/delSyncgroup' // 删除同步信息(用户组)
const syncOrdergroup = 'group/syncOrdergroup' // 同步信息排序(用户组)
const syncFiltersgroup = 'group/syncFiltersgroup' // 同步信息筛选(用户组)
const changeLoading = 'setting/changeLoading'
const syncValue = 'group/Syncvalue' // search
const getCurrent = 'group/getCurrent' // 设置当前页码
const getPageize = 'group/getPageize' // 设置页码大小e

export default {
  state: {
    optionName: [], // 用户组名称
    optionSelect: [],
    group: [], // 用户组信息
    groupDetail: { 'role_name': [] },
    rightGroup: [{ 'user_group_name': '' }],
    roleName: [], // 角色信息
    role: [],
    role_id: '',
    suregroup: '',
    groupDetailflage: false,
    upGroupflag: false,
    addGroupflag: false,
    space: true,
    groupCount: '',
    syncDategroup: {
      'sorter': {
        'field': 'zr_time', // 排序的字段
        'order': 'descend' // or 'desc'升序还是降序
      },
      'filters': {
        'fuser_group_name': [] // 需要过滤的字段
      },
      'dict': { 'type': 'add' },
      'addtable': [], // 用户信息列表,
      'uptable': [],
      'deltable': []
    },
    syncLog: {
      'pagination': { // 分页
        'current': 1, // 当前页码，类型：number
        'pageSize': 20 // 单页面数据条数，类型：number
      },
      'search': { 'value': '' },
      'total': null,
      'filters': {
        'fuser_group_name': [] // 需要过滤的字段
      },
      'sorter': {
        'field': 'zr_time', // 排序的字段
        'order': 'descend' // or 'desc'升序还是降序
      },
      'dict': { 'type': 'add' },
      'table': [] // 用户信息列表
    },
    loading: false, // 加载状态
    userDate: {
      'pagination': { // 分页
        'current': 1, // 当前页码，类型：number
        'pageSize': 20 // 单页面数据条数，类型：number
      },
      'search': { 'value': '' },
      'total': null,
      'sorter': {
        'field': 'auser_group_name', // 排序的字段
        'order': 'descend' // or 'desc'升序还是降序
      },
      'filters': {
        'auser_group_name': [] // 需要过滤的字段
      },
      'table': [{ 'auser_group_name': '' }] // 用户信息列表
    }
  },
  actions: {
    [getSynclog] ({ commit, state }, data) {
      let form = JSON.parse(JSON.stringify(state.syncLog))
      form.dict.type = data.type
      $http.option('/api/group/snyclog', form).then(({ status, data, msg } = {}) => {
        if (status === 'success') {
          commit(setSynclog, data)
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取信息失败'
          })
        }
      })
    },
    [getOptions] ({ commit }, data) {
      $http.get('/api/user/options').then(({ status, msg } = {}) => {
        if (status === 'success') {
          const option = []
          if (data === 1) {
            for (let i = 0; i < msg.length; i++) {
              let groupName = {}
              groupName['value'] = msg[i].user_group_id
              groupName['text'] = msg[i].user_group_name
              option.push(groupName)
            }
            commit(setOptions, option)
          } else {
            commit(setOptionselect, msg)
          }
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取信息失败'
          })
        }
      })
    },
    [getGroup] ({ commit, state }) {
      commit(changeLoading, true)
      const form = state.userDate
      $http.option('/api/user/usersgroup', form).then(({ status, msg } = {}) => {
        if (status === 'success') {
          commit(changeLoading, false)
          commit(setGroup, msg)
        } else if (status === 'notallow') {
          Notification.error({
            title: '没有操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取信息失败'
          })
        }
      })
    },
    [getGroupdetail] ({ commit }, data) {
      $http.post('/api/user/groupdetailed', data).then(({ status, msg } = {}) => {
        if (status === 'success') {
          let data = {
            'user_group_id': msg[0].user_group_id,
            'role_name': msg[0].role_name.split(','),
            'user_group_name': msg[0].user_group_name
          }
          commit(setGroupdetail, data)
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取信息失败'
          })
        }
      })
    },
    [getRightdetail] ({ commit }, data) {
      $http.post('/api/user/rightdetailed', data).then(({ status, msg } = {}) => {
        if (status === 'success') {
          commit(setRightdetail, msg)
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取信息失败'
          })
        }
      })
    },
    [getRole] ({ commit }) {
      $http.post('/api/user/role').then(({ status, msg } = {}) => {
        if (status === 'success') {
          const list = []
          for (let i = 0; i < msg.length; i++) {
            let rname = {}
            rname['value'] = msg[i].role_name
            rname['text'] = msg[i].role_name
            rname['group'] = msg[i].string_agg
            rname['key'] = msg[i].role_id
            list.push(rname)
          }
          commit(setRole, list)
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取信息失败'
          })
        }
      })
    },
    [addGroup] ({ commit, dispatch }, data) {
      commit(changeLoading, true)
      $http.post('api/user/addgroup', data).then(({ status, msg } = {}) => {
        if (status === 'success') {
          commit(changeLoading, false)
          dispatch(getOptions)
          Notification.success({
            title: '新增用户组成功'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '新增用户组失败'
          })
        }
      })
    },
    [delUsergroup] ({ commit, dispatch }, data) {
      commit(changeLoading, true)
      $http.post('api/user/delgroup', data).then(({ status, msg } = {}) => {
        if (status === 'success') {
          dispatch(getOptions)
          commit(changeLoading, false)
          Notification.success({
            title: '删除用户组成功'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '删除用户组失败'
          })
        }
      })
    },
    [updateGroup] ({ commit, dispatch }, data) {
      commit(changeLoading, true)
      $http.post('api/user/updategroup', data).then(({ status, msg } = {}) => {
        if (status === 'success') {
          dispatch(getOptions)
          commit(changeLoading, false)
          Notification.success({
            title: '修改用户组成功'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '修改用户组失败'
          })
        }
      })
    },
    [getRolelist] ({ commit }, data) {
      $http.post('/api/department/role', data).then(({ status, msg } = {}) => {
        if (status === 'success') {
          commit(setRolelist, msg)
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取信息失败'
          })
        }
      })
    },
    [getRoleid] ({ dispatch, commit }, data) {
      $http.post('/api/user/roleid', data).then(({ status, msg } = {}) => {
        if (status === 'success') {
          commit(setRoleid, msg[0].role_name)
          var re = new RegExp(',', 'g')
          var data = "'" + msg[0].group_id.replace(re, "','") + "'"
          dispatch(getRolelist, { 'role_group_id': data })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取信息失败'
          })
        }
      })
    },
    [getSyncgroup] ({ commit }) {
      $http.post('/api/group/snyccount').then(({ status, msg } = {}) => {
        if (status === 'success') {
          commit(setSyncgroup, msg)
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取信息失败'
          })
        }
      })
    },
    [getSyncmessagegroup] ({ commit, state }, data) {
      let form = JSON.parse(JSON.stringify(state.syncDategroup))
      form.dict.type = data.type
      $http.option('/api/group/snycmessage', form).then(({ status, msg } = {}) => {
        if (status === 'success') {
          commit(setSyncmessagegroup, { 'type': data, 'data': msg })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '获取信息失败'
          })
        }
      })
    },
    [delSyncgroup] ({ commit, state, dispatch }, data) {
      $http.option('/api/group/delsnyccount').then(({ status, msg } = {}) => {
        if (status === 'success') {
          dispatch('group/getSyncmessagegroup', { 'type': 'add' })
	        dispatch('group/getSyncmessagegroup', { 'type': 'update' })
	        dispatch('group/getSyncmessagegroup', { 'type': 'delete' })
          Notification.success({
            title: '删除成功'
          })
        } else if (status === 'notallow') {
          Notification.error({
            title: '不具备操作权限'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '删除失败'
          })
        }
      })
    }
  },
  mutations: {
    [setSynclog] (state, value) {
      state.syncLog.table = value.dataSource
      state.syncLog.total = value.total
      state.syncLog.pagination.current = value.current
    },
    [setOptions] (state, value) {
      state.optionName = value
    },
    [setOptionselect] (state, value) {
      state.optionSelect = value
    },
    [setGroup] (state, value) {
      state.userDate.table = value.dataSource
      state.userDate.pagination.current = value.current
      state.userDate.total = value.total
    },
    [setGroupdetail] (state, value) {
      state.groupDetail = value
    },
    [setRightdetail] (state, value) {
      state.rightGroup = value
    },
    [setRole] (state, value) {
      state.roleName = value
    },
    [setRolelist] (state, value) {
      state.role = value
    },
    [setRoleid] (state, value) {
      state.role_id = value
    },
    [addGroupflag] (state, value) {
      state.addGroupflag = value
    },
    [upGroupflag] (state, value) {
      state.upGroupflag = value
    },
    [groupDetailflage] (state, value) {
      state.groupDetailflage = value
    },
    [space] (state, value) {
      state.space = value
    },
    [setSyncgroup] (state, value) {
      state.groupCount = value.count
    },
    [setSyncmessagegroup] (state, value) {
      if (value.type.type === 'add') {
        state.syncDategroup.addtable = value.data
      } else if (value.type.type === 'update') {
        state.syncDategroup.uptable = value.data
      } else {
        state.syncDategroup.deltable = value.data
      }
    },
    [syncOrdergroup] (state, value) {
      state.syncDategroup.sorter.field = value.prop
      state.syncDategroup.sorter.order = value.order
      state.syncLog.sorter.field = value.prop
      state.syncLog.sorter.order = value.order
    },
    [changeLoading] (state, value) {
      state.loading = value
    },
    [syncFiltersgroup] (state, value) {
      state.syncLog.search.value = value
    },
    [syncValue] (state, value) {
      state.syncLog.search.value = value
      state.userDate.search.value = value
    },
    [getCurrent] (state, val) {
      state.userDate.pagination.current = val
      state.syncLog.pagination.current = val
    },
    [getPageize] (state, val) {
      state.userDate.pagination.pageSize = val
      state.syncLog.pagination.pageSize = val
    }
  }
}
